// cd /projects/hsieh_project/proj_201809/code_2_202011/
// qstata city_emp_top_mkt_firms.do &

set linesize 255
capture log close
log using /projects/hsieh_project/proj_201809/code_2_202011/city_emp_top_mkt_firms_log, replace text

clear all
cd /projects/

di "Started at $S_TIME $S_DATE"

global rev_date: display %tdYYNNDD date("$S_DATE", "DMY")
di "${rev_date}"

global dir_proj "/projects/hsieh_project/proj_201809/"

global dir_do "${dir_proj}/code_2_202011/"
global dir_data "${dir_proj}/data/"
global dir_out "${dir_proj}/output/202011_main/"
noi capture mkdir ${dir_out}
global dir_outf "${dir_out}/city_emp_top_mkt_firms/"
noi capture mkdir ${dir_outf}

global year1 = 1977
global year2 = 2013

global gl_perc = "1 10"
local l_perc = "1 10"

global lmkt_labels = "n_est n_msa1983"

do "/projects/hsieh_project/code_0_general/f_rounding.do"
do "/projects/hsieh_project/code_0_general/f_reg.do"

global ds_ind "${dir_data}/ind_sum_all"

//==============================================================================
/*
Author: Adarsh Kumar
Objective: Calculate & Plot change in log(share of top 1, 10% firms' employment in MSA)
*/

use ${dir_data}/cityind_top_mkt_est_sum.dta, clear
merge 1:1 year sector ch_ind msa1983 emp_cind est_cind pay_cind n_cind using ${dir_data}/cityind_top_mkt_msa1983_sum.dta, assert(match) nogen

bys year msa1983: egen emp_msa1983 = total(emp_cind)
gen emp_w = emp_cind / emp_msa1983

*Need to adjust weights for industries that contain fewer than 100/`vperc' firms:
merge m:1 year ch_ind using $ds_ind, keepus(n_ind)
keep if _merge == 3
drop _merge  

foreach perc in $gl_perc {
	gen emp_cind_`perc' = emp_cind
	replace emp_cind_`perc' = 0 if n_ind < 100 / `perc'
	bys year msa1983: egen den_w_`perc' = total(emp_cind_`perc')	
	gen emp_w_`perc' = emp_cind_`perc' / den_w_`perc'
}

sort msa1983 year
bys msa1983: gen emp_msa1983_1977 = emp_msa1983[1]

*Calculate share of top 1,10% firms' employment in each MSA-ch_ind:
foreach vmkt in $lmkt_labels {
	foreach perc in $gl_perc {
		gen emp_ind_`vmkt'_`perc'_sh = emp_ind_`vmkt'_`perc' / emp_cind
		gen emp_ind_`vmkt'_`perc'_sh_w = emp_ind_`vmkt'_`perc'_sh * emp_w_`perc' 
	}
}

collapse (sum) emp_ind*_sh_w emp_w emp_ind_*1 emp_ind_*10 emp_cind (mean) emp_msa1983_1977, by(year msa1983)

foreach vmkt in $lmkt_labels {
	foreach perc in $gl_perc {
		gen ln_emp_ind_`vmkt'_`perc'_sh_w = ln(emp_ind_`vmkt'_`perc'_sh_w)
		replace ln_emp_ind_`vmkt'_`perc'_sh_w = -ln_emp_ind_`vmkt'_`perc'_sh_w if year == $year1	
	}
}

gen ln_emp_msa1983_1977 = ln(emp_msa1983_1977)

capture program drop f_fig_levels
program f_fig_levels
args y y_lab y_save

/*
twoway (lpolyci `y' emp_msa1983_1977 if year == $year1, level(99) clc(navy) ciplot(rline) clw(medthick) fc(none) alcolor(dkgreen) alpattern(dash) alwidth(thin)) (lpolyci `y' emp_msa1983_1977 if year == $year2, level(99) clc(orange) ciplot(rline) clw(medthick) fc(none) alcolor(red) alpattern(dash) alwidth(thin)), ///
	ytitle("`y_lab'") xtitle("City Employment in ${year1}") ///
	legend(order(2 "${year1}" 4 "${year2}")) note("99% CI")
graph export "${dir_outf}/Levels_`y_save'_X_ln_c_emp_${year1}_msa1983.png", replace width(3000) height(2000)
*/

twoway (lpolyci `y' ln_emp_msa1983_1977 if year == $year1, level(99) clc(navy) ciplot(rline) clw(medthick) fc(none) alcolor(dkgreen) alpattern(dash) alwidth(thin)) (lpolyci `y' ln_emp_msa1983_1977 if year == $year2, level(99) clc(orange) ciplot(rline) clw(medthick) fc(none) alcolor(red) alpattern(dash) alwidth(thin)), ///
	ytitle("`y_lab'") xtitle("Ln (City Employment in ${year1})") ///
	legend(order(2 "${year1}" 4 "${year2}")) note("99% CI")
graph export "${dir_outf}/Levels_`y_save'_X_ln_c_ln_emp_${year1}_msa1983.png", replace width(3000) height(2000)



end

f_fig_levels "emp_ind_n_est_1_sh_w" "Emp. Share of top 1% Firms by # Est" "emp_est_1"
f_fig_levels "emp_ind_n_est_10_sh_w" "Emp. Share of top 10% Firms by # Est" "emp_est_10"
f_fig_levels "emp_ind_n_msa1983_1_sh_w" "Emp. Share of top 1% Firms by # MSA1983" "emp_msa1983_1"
f_fig_levels "emp_ind_n_msa1983_10_sh_w" "Emp. Share of top 10% Firms by # MSA1983" "emp_msa1983_10"

*For summary table: 
preserve
collapse (mean) emp_ind_n_est_10_sh_w emp_ind_n_msa1983_10_sh_w (count) msa1983, by(year)
save ${dir_out}/summary_stats/E_msa1983_top_est_share, replace
restore



collapse (sum) ln_emp_ind_*_sh_w (mean) emp_msa1983_1977, by(msa1983)
gen ln_emp_msa1983_1977 = ln(emp_msa1983_1977)

*PLots
*Lpoly plots w/ scatter: 
program f_fig_wo_scatter
args y y_lab y_save

twoway (lpolyci `y' ln_emp_msa1983_1977, level(99) clc(navy) clw(medthick) fc(none) alcolor(gs12)), ///
	ytitle("`y_lab'") xtitle("ln(City Employment in ${year1})") ///
	legend(off) ///
	note("99% CI")
graph export "${dir_outf}/Chg_`y_save'_X_ln_c_emp_${year1}_msa1983.png", replace width(3000) height(2000)

reg `y' ln_emp_msa1983_1977
f_reg_append "fr_reg" "ln_emp_msa1983_1977"

end

f_reg_create "fr_reg" "1"

f_fig_wo_scatter "ln_emp_ind_n_est_1_sh_w" "Chg in Ln(Emp. Share of top 1% Firms by # Est)" "ln_emp_est_1"
f_fig_wo_scatter "ln_emp_ind_n_est_10_sh_w" "Chg in Ln(Emp. Share of top 10% Firms by # Est)" "ln_emp_est_10"
f_fig_wo_scatter "ln_emp_ind_n_msa1983_1_sh_w" "Chg in Ln(Emp. Share of top 1% Firms by # MSA1983)" "ln_emp_msa1983_1"
f_fig_wo_scatter "ln_emp_ind_n_msa1983_10_sh_w" "Chg in Ln(Emp. Share of top 10% Firms by # MSA1983)" "ln_emp_msa1983_10"
*/
capture noi log close
// End of do file
